package com.cssw.fyzb.core.dao.tesk;


import com.cssw.fyzb.core.model.dodb.RuleInst;
import com.cssw.fyzb.core.util.DateUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author
 */
@Repository
public class RuleInstDaoImpl implements RuleInstDao {

    @Override
    public RuleInst getById(Long instId) {
        return RuleInst.dao.findById(instId);
    }

    @Override
    public Integer getRuleInst(Long userId, Long taskId) {
        StringBuilder sqlParse = new StringBuilder();
        sqlParse.append("select count(1) from t_rule_inst where ");
        sqlParse.append(" user_id=? and task_id=? ");
        return Db.queryInt(sqlParse.toString(), userId, taskId);
    }

    @Override
    public RuleInst getDayTask(Long userId) {
        Map<String, Object> parm = new HashMap<>();
        parm.put("userId", userId);
        parm.put("status",RuleInst.STATUS_ONGOING);
        SqlParse sqlParse = new SqlParse(parm);
        sqlParse.addSQL("select * from t_rule_inst where ");
        sqlParse.addSQL(" user_id=:userId and status=:status");
        return RuleInst.dao.findFirst(sqlParse);
    }

    @Override
    public Long save(RuleInst ruleInst) {
        ruleInst.save();
        return ruleInst.getInstId();
    }

    @Override
    public void update(RuleInst ruleInst) {
        ruleInst.update();
    }

    @Override
    public List<RuleInst> listRuleInst(Long userId, String status) {
        Map<String, Object> parm = new HashMap<>();
        parm.put("userId", userId);
        parm.put("status", status);
        SqlParse sqlParse = new SqlParse(parm);
        sqlParse.addSQL("select t1.*,t2.task_name,t2.price,t2.task_time,t2.produce_day from t_rule_inst t1 ");
        sqlParse.addSQL(" left join d_task t2 on t1.task_id=t2.task_id where t1.status=:status and t1.user_id=:userId ");
        return RuleInst.dao.find(sqlParse);
    }

    @Override
    public List<RuleInst> listValidRuleInst(Long userId) {
        Map<String, Object> parm = new HashMap<>();
        parm.put("userId", userId);
        SqlParse sqlParse = new SqlParse(parm);
        sqlParse.addSQL("select t1.*,t2.task_time from t_rule_inst t1 left join d_task t2 on t1.task_id=t2.task_id");
        sqlParse.addSQL(" where t1.user_id=:userId and eff_date <= now() and exp_date >= now() ");
        return RuleInst.dao.find(sqlParse);
    }

    @Override
    public Page<RuleInst> myTaskList(Long userId, String status, int pageNumber, int pageSize) {
        Map<String, Object> parm = new HashMap<>();
        parm.put("userId", userId);
        parm.put("status", status);
        SqlParse sqlParse = new SqlParse(parm);
        sqlParse.addSQL("select t1.*,t2.task_name,t2.price,t2.task_time,t2.produce_day,t2.task_icon from t_rule_inst t1 ");
        sqlParse.addSQL(" left join d_task t2 on t1.task_id=t2.task_id where t1.status=:status  and t1.user_id=:userId ");
        return RuleInst.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<RuleInst> listDayRuleInst(Long userId, String status, int pageNumber, int pageSize) {
        Map<String, Object> parm = new HashMap<>();
        parm.put("userId", userId);
        parm.put("status", status);
        //取今天的任务进行计算
        Date date = new Date();
        parm.put("date", DateUtil.format(date));
        SqlParse sqlParse = new SqlParse(parm);
        sqlParse.addSQL("select t1.*,t2.task_name,t2.price,t2.task_time,t2.produce_day,t2.task_icon from t_rule_inst t1 ");
        sqlParse.addSQL(" left join d_task t2 on t1.task_id=t2.task_id where t1.status=:status  and t1.user_id=:userId ");
        sqlParse.addSQL(" and t1.exp_date>=:date and t1.eff_date <=:date ");
        return RuleInst.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public RuleInst getUserTotalTiem(Long userId) {
        StringBuilder str = new StringBuilder();
        Date date = new Date();
        System.out.println(DateUtil.format(date));
        str.append("select sum(t2.task_time)total_tiem from t_rule_inst t1 left join  d_task t2 ");
        str.append("on t1.task_id=t2.task_id where t1.user_id=?  and t1.eff_date<=? and t1.exp_date>=? and status=? GROUP BY t1.user_id  ");
        return RuleInst.dao.findFirst(str.toString(), userId, DateUtil.format(date), DateUtil.format(date), RuleInst.STATUS_ONGOING);
    }

    @Override
    public List<RuleInst> getTotalTime() {
        Map<String, Object> parm = new HashMap<>();
        //取今天的任务进行计算
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        parm.put("date", formatter.format(date));
        SqlParse sqlParse = new SqlParse(parm);
        sqlParse.addSQL("select t1.*,sum(t2.task_time) total_tiem from t_rule_inst t1 left join  d_task t2 ");
        sqlParse.addSQL("on t1.task_id=t2.task_id where t1.eff_date<=date and t1.exp_date>=:date   GROUP BY t1.user_id  ");
        return RuleInst.dao.find(sqlParse);
    }

    @Override
    public Long updateRuleInstDao(RuleInst ruleInst) {
        boolean update = ruleInst.update();
        return ruleInst.getInstId();
    }

    @Override
    public Integer myClaim(Long userId, Long taskId) {
        StringBuilder str = new StringBuilder();
        str.append("select count(1) from t_rule_inst where task_id=? and user_id=? ");
        return Db.queryInt(str.toString(), taskId, userId);
    }
}
